package com.yz.administrator.jiukuaijiu.zhekou.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.yz.administrator.jiukuaijiu.R;
import com.yz.administrator.jiukuaijiu.net.MyHttpUtils;
import com.yz.administrator.jiukuaijiu.ui.shop.ShopAdapter;
import com.yz.administrator.jiukuaijiu.ui.shop.ShopBean;
import com.yz.administrator.jiukuaijiu.utils.HttpResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * A simple {@link Fragment} subclass.
 */
public class FourthFragment extends Fragment {

    private RecyclerView recyView;
    private ArrayList<ShopBean> datas;
    private ShopAdapter adapter;
    public FourthFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fourth, container, false);
        init(view);

        return view;
    }


    private void init(View view) {
        recyView = (RecyclerView) view.findViewById(R.id.shop_recyclerview);
        //   recyView.setHasFixedSize(true);
        LinearLayoutManager manager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
        //recyView.setLayoutManager(new LinearLayoutManager(this));
        recyView.setLayoutManager(manager);
        datas = new ArrayList<>();
        adapter = new ShopAdapter(getContext(), datas);
        recyView.setAdapter(adapter);
        loadData();
    }





    private static final String Base_url = "http://cloud.yijia.com/outdata/data.php";

    private void loadData() {

        final HashMap<String, String> params = new HashMap<>();
        params.put("page", "1");
        params.put("limit", "10");

        MyHttpUtils.getInstance().get(Base_url, "", params, new HttpResponse() {
            @Override
            public void onResponse(String data) {

                Log.e("====", "===params==" + params);
                // Log.e("====","===data==="+data);

                try {
                    JSONObject jsonObject = new JSONObject(data);
                    JSONArray array = jsonObject.optJSONArray("data");

                    Gson gson = new Gson();
                    for (int i = 0; i < array.length(); i++) {
                        String s = array.get(i).toString();
                        datas.add(gson.fromJson(s, ShopBean.class));
                    }
                    adapter.notifyDataSetChanged();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String msg) {
                Log.e("====", "===data" + msg);
                Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
            }
        });

    }

}
